#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>


int main(int argc, char *argv[])
{
	if (argc == 1 ) {  // no programs
		printf("EINVAL\n");
    return EINVAL;
  	}
	else if  (argc == 2 ){ //./pipe ls  one program
		int exec_return = execlp(argv[1],argv[1],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			  }
	}
	else if  (argc == 3 ){ //.pipe ls cat
		int fd[2];
		pipe(fd);
  		if (pipe(fd) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;  //pipe error
  		}
		pid_t pid1 =fork(); //create
		if (pid1 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid1 == 0) {  // Child for ls 
			dup2(fd[1],1);  //"1" means stdout
			close(fd[1]);
			close(fd[0]);
			int exec_return = execlp(argv[1],argv[1],0);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
			return 0;
		}
//parent
		wait(NULL);
		close(fd[1]);
		pid_t pid2 = fork();
		if (pid2 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
			}
		if (pid2 == 0){  // Child for cat
			dup2(fd[0],0);
			close(fd[0]);
			int exec_return = execlp(argv[2],argv[2],0);
  			if (exec_return == -1) {
    				exec_return = errno;
    				perror("execlp failed");
    				return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd[0]);
		}	
	else if  (argc == 4 ){ //.pipe ls cat wc
		int fd[2];
		pipe(fd);//1st pipe
  		if (pipe(fd) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;  //pipe error
  		}
		pid_t pid1 =fork(); //create
		if (pid1 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid1 == 0) {  // Child for ls 
			close(fd[0]);
			dup2(fd[1],1);
			close(fd[1]);//"1" means stdout
			int exec_return = execlp(argv[1],argv[1],0);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
			return 0;
		}
//parent
		wait(NULL);
		close(fd[1]);
		int fd1[2];
		pipe(fd1);
		if (pipe(fd1) == -1) {
    			int err = errno;
    			perror("pipe");
    			return err;  //pipe error
  			}
		pid_t pid2 = fork();
		if (pid2 == -1) { //fork error
				int err = errno;
				perror("fork failed");
				return err;
			}
		if (pid2 == 0){  // Child for cat  0 to fd[0], 1 to fd1[1]
				dup2(fd[0],0);
				close(fd[0]);
				dup2(fd1[1],1);
				close(fd1[1]);
				close(fd1[0]);
				int exec_return = execlp(argv[2],argv[2],NULL);
  				if (exec_return == -1) {
    				exec_return = errno;
    				perror("execlp failed");
    				return exec_return;
				}
			}
//parent
		wait(NULL);
		close(fd[0]);
		close(fd1[1]);
		pid_t pid3 = fork();
		if (pid3 == -1) { //fork error
						int err = errno;
						perror("fork failed");
						return err;
					}
		if (pid3 == 0){ //Child for wc
			dup2(fd1[0],0);
			close(fd1[0]);
			int exec_return = execlp(argv[3],argv[3],0);
  			if (exec_return == -1) {
    						exec_return = errno;
    						perror("execlp failed");
    						return exec_return;
						}
		}
//parent
		wait(NULL);
		close(fd1[0]);
	}
	else if  (argc == 5 ){
		int fd[2];
		pipe(fd);
  		if (pipe(fd) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;
		}      //pipe error
		pid_t pid1 =fork(); //create
		if (pid1 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid1 == 0) {  // Child for ls 
			close(fd[0]);
			dup2(fd[1],1);//"1" means stdout
			close(fd[1]); //Child1 RW closed
			int exec_return = execlp(argv[1],argv[1],0);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
			return 0;
		}
//parent
		wait(NULL);  // wait for child1
		close(fd[1]);  //1st pipe write close
		int fd1[2];
		pipe(fd1);//2nd pipe RW open
		if (pipe(fd1) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;  //pipe error
  		}
		pid_t pid2 = fork();
		if (pid2 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid2 == 0){  // Child for cat  0 to fd[0], 1 to fd1[1]
			dup2(fd[0],0);
			close(fd[0]);
			dup2(fd1[1],1);
			close(fd1[1]);
			close(fd1[0]); //child2 RW closed
			int exec_return = execlp(argv[2],argv[2],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd[0]);  //1st pipe RW closed
		close(fd1[1]);//2nd pipe write close
		int fd2[2];
		pipe(fd2);
		if (pipe(fd2) == -1){
			int err = errno;
			perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid3 = fork();
		if (pid3 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid3 == 0){ //Child for wc
			dup2(fd1[0],0);
			close(fd1[0]);
			dup2(fd2[1],1);
			close(fd2[0]);
			close(fd2[1]); //child3 RW closed
			int exec_return = execlp(argv[3],argv[3],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd1[0]); //2nd pipe RW closed
		close(fd2[1]); //3rd pipe write close
		pid_t pid4 = fork();
		if (pid4 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid4 == 0 ) { // child for 4th process
			dup2(fd2[0],0);
			close(fd2[0]);
			int exec_return = execlp(argv[4],argv[4],NULL);
			if (exec_return == -1) {
				exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd2[0]);//3rd pipe RW closed
						}
	else if  (argc == 6 ){
		int fd[2];
		pipe(fd);
  		if (pipe(fd) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;
		}      //pipe error
		pid_t pid1 =fork(); //create
		if (pid1 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid1 == 0) {  // Child for ls 
			close(fd[0]);
			dup2(fd[1],1);//"1" means stdout
			close(fd[1]); //Child1 RW closed
			int exec_return = execlp(argv[1],argv[1],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
			return 0;
		}
//parent
		wait(NULL);  // wait for child1
		close(fd[1]);  //1st pipe write close
		int fd1[2];
		pipe(fd1);//2nd pipe RW open
		if (pipe(fd1) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;  //pipe error
  		}
		pid_t pid2 = fork();
		if (pid2 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid2 == 0){  // Child for cat  0 to fd[0], 1 to fd1[1]
			dup2(fd[0],0);
			close(fd[0]);
			dup2(fd1[1],1);
			close(fd1[1]);
			close(fd1[0]); //child2 RW closed
			int exec_return = execlp(argv[2],argv[2],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd[0]);  //1st pipe RW closed
		close(fd1[1]);//2nd pipe write close
		int fd2[2];
		pipe(fd2);
		if (pipe(fd2) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid3 = fork();
		if (pid3 == -1) { //fork error
		    int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid3 == 0){ //Child for wc
			dup2(fd1[0],0);
			close(fd1[0]);
			dup2(fd2[1],1);
			close(fd2[0]);
			close(fd2[1]); //child3 RW closed
			int exec_return = execlp(argv[3],argv[3],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd1[0]); //2nd pipe RW closed
		close(fd2[1]); //3rd pipe write close
		int fd3[2];
		pipe(fd3);
		if (pipe(fd3) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid4 = fork();
		if (pid4 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid4 == 0 ) { // child for 4th process
			dup2(fd2[0],0);
			close(fd2[0]);
			dup2(fd3[1],1);
			close(fd3[0]);
			close(fd3[1]);
			int exec_return = execlp(argv[4],argv[4],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd2[0]);//3rd pipe RW closed
		close(fd3[1]);
		pid_t pid5 = fork();
		if (pid5 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid5 == 0 ) {
			dup2(fd3[0],0);
			close(fd3[0]);
			int exec_return = execlp(argv[5],argv[5],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent 
		wait(NULL);
		close(fd3[0]);
	}
  	else if  (argc == 7 ){
		int fd[2];
		pipe(fd);
  		if (pipe(fd) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;
		}      //pipe error
		pid_t pid1 =fork(); //create
		if (pid1 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid1 == 0) {  // Child for ls 
			close(fd[0]);
			dup2(fd[1],1);//"1" means stdout
			close(fd[1]); //Child1 RW closed
			int exec_return = execlp(argv[1],argv[1],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
			return 0;
		}
//parent
		wait(NULL);  // wait for child1
		close(fd[1]);  //1st pipe write close
		int fd1[2];
		pipe(fd1);//2nd pipe RW open
		if (pipe(fd1) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;  //pipe error
  		}
		pid_t pid2 = fork();
		if (pid2 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid2 == 0){  // Child for cat  0 to fd[0], 1 to fd1[1]
			dup2(fd[0],0);
			close(fd[0]);
			dup2(fd1[1],1);
			close(fd1[1]);
			close(fd1[0]); //child2 RW closed
			int exec_return = execlp(argv[2],argv[2],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd[0]);  //1st pipe RW closed
		close(fd1[1]);//2nd pipe write close
		int fd2[2];
		pipe(fd2);
		if (pipe(fd2) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid3 = fork();
		if (pid3 == -1) { //fork error
		    int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid3 == 0){ //Child for wc
			dup2(fd1[0],0);
			close(fd1[0]);
			dup2(fd2[1],1);
			close(fd2[0]);
			close(fd2[1]); //child3 RW closed
			int exec_return = execlp(argv[3],argv[3],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd1[0]); //2nd pipe RW closed
		close(fd2[1]); //3rd pipe write close
		int fd3[2];
		pipe(fd3);
		if (pipe(fd3) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid4 = fork();
		if (pid4 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid4 == 0 ) { // child for 4th process
			dup2(fd2[0],0);
			close(fd2[0]);
			dup2(fd3[1],1);
			close(fd3[0]);
			close(fd3[1]);
			int exec_return = execlp(argv[4],argv[4],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd2[0]);//3rd pipe RW closed
		close(fd3[1]);//4rd pipe write close
		int fd4[2];
		pipe(fd4);
		if (pipe(fd4) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid5 = fork();
		if (pid5 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid5 == 0 ) {
			dup2(fd3[0],0);
			close(fd3[0]);
			dup2(fd4[1],1);
			close(fd4[0]);
			close(fd4[1]);
			int exec_return = execlp(argv[5],argv[5],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent 
		wait(NULL);
		close(fd3[0]);
		close(fd4[1]);
		pid_t pid6 = fork();
		if (pid6 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid6 == 0 ) {
			dup2(fd4[0],0);
			close(fd4[0]);
			int exec_return = execlp(argv[6],argv[6],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent 
		wait(NULL);
		close(fd4[0]);
	}
	else if  (argc == 8 ){
		int fd[2];
		pipe(fd);
  		if (pipe(fd) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;
		}      //pipe error
		pid_t pid1 =fork(); //create
		if (pid1 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid1 == 0) {  // Child for ls 
			close(fd[0]);
			dup2(fd[1],1);//"1" means stdout
			close(fd[1]); //Child1 RW closed
			int exec_return = execlp(argv[1],argv[1],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
			return 0;
		}
//parent
		wait(NULL);  // wait for child1
		close(fd[1]);  //1st pipe write close
		int fd1[2];
		pipe(fd1);//2nd pipe RW open
		if (pipe(fd1) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;  //pipe error
  		}
		pid_t pid2 = fork();
		if (pid2 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid2 == 0){  // Child for cat  0 to fd[0], 1 to fd1[1]
			dup2(fd[0],0);
			close(fd[0]);
			dup2(fd1[1],1);
			close(fd1[1]);
			close(fd1[0]); //child2 RW closed
			int exec_return = execlp(argv[2],argv[2],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd[0]);  //1st pipe RW closed
		close(fd1[1]);//2nd pipe write close
		int fd2[2];
		pipe(fd2);
		if (pipe(fd2) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid3 = fork();
		if (pid3 == -1) { //fork error
		    int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid3 == 0){ //Child for wc
			dup2(fd1[0],0);
			close(fd1[0]);
			dup2(fd2[1],1);
			close(fd2[0]);
			close(fd2[1]); //child3 RW closed
			int exec_return = execlp(argv[3],argv[3],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd1[0]); //2nd pipe RW closed
		close(fd2[1]); //3rd pipe write close
		int fd3[2];
		pipe(fd3);
		if (pipe(fd3) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid4 = fork();
		if (pid4 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid4 == 0 ) { // child for 4th process
			dup2(fd2[0],0);
			close(fd2[0]);
			dup2(fd3[1],1);
			close(fd3[0]);
			close(fd3[1]);
			int exec_return = execlp(argv[4],argv[4],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd2[0]);//3rd pipe RW closed
		close(fd3[1]);//4rd pipe write close
		int fd4[2];
		pipe(fd4);
		if (pipe(fd4) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid5 = fork();
		if (pid5 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid5 == 0 ) {
			dup2(fd3[0],0);
			close(fd3[0]);
			dup2(fd4[1],1);
			close(fd4[0]);
			close(fd4[1]);
			int exec_return = execlp(argv[5],argv[5],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent 
		wait(NULL);
		close(fd3[0]);
		close(fd4[1]);
		int fd5[2];
		pipe(fd5);
		if (pipe(fd5) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid6 = fork();
		if (pid6 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid6 == 0 ) {
			dup2(fd4[0],0);
			close(fd4[0]);
			dup2(fd5[1],1);
			close(fd5[0]);
			close(fd5[1]);
			int exec_return = execlp(argv[6],argv[6],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent 
		wait(NULL);
		close(fd4[0]);
		close(fd5[1]);
		pid_t pid7 = fork();
		if (pid7 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid7 == 0 ) {
			dup2(fd5[0],0);
			close(fd5[0]);
			int exec_return = execlp(argv[7],argv[7],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent 
		wait(NULL);
		close(fd5[0]);
	}
	else if  (argc == 9 ){
		int fd[2];
		pipe(fd);
  		if (pipe(fd) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;
		}      //pipe error
		pid_t pid1 =fork(); //create
		if (pid1 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid1 == 0) {  // Child for ls 
			close(fd[0]);
			dup2(fd[1],1);//"1" means stdout
			close(fd[1]); //Child1 RW closed
			int exec_return = execlp(argv[1],argv[1],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
			return 0;
		}
//parent
		wait(NULL);  // wait for child1
		close(fd[1]);  //1st pipe write close
		int fd1[2];
		pipe(fd1);//2nd pipe RW open
		if (pipe(fd1) == -1) {
    		int err = errno;
    		perror("pipe");
    		return err;  //pipe error
  		}
		pid_t pid2 = fork();
		if (pid2 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid2 == 0){  // Child for cat  0 to fd[0], 1 to fd1[1]
			dup2(fd[0],0);
			close(fd[0]);
			dup2(fd1[1],1);
			close(fd1[1]);
			close(fd1[0]); //child2 RW closed
			int exec_return = execlp(argv[2],argv[2],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd[0]);  //1st pipe RW closed
		close(fd1[1]);//2nd pipe write close
		int fd2[2];
		pipe(fd2);
		if (pipe(fd2) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid3 = fork();
		if (pid3 == -1) { //fork error
		    int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid3 == 0){ //Child for wc
			dup2(fd1[0],0);
			close(fd1[0]);
			dup2(fd2[1],1);
			close(fd2[0]);
			close(fd2[1]); //child3 RW closed
			int exec_return = execlp(argv[3],argv[3],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd1[0]); //2nd pipe RW closed
		close(fd2[1]); //3rd pipe write close
		int fd3[2];
		pipe(fd3);
		if (pipe(fd3) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid4 = fork();
		if (pid4 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid4 == 0 ) { // child for 4th process
			dup2(fd2[0],0);
			close(fd2[0]);
			dup2(fd3[1],1);
			close(fd3[0]);
			close(fd3[1]);
			int exec_return = execlp(argv[4],argv[4],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent
		wait(NULL);
		close(fd2[0]);//3rd pipe RW closed
		close(fd3[1]);//4rd pipe write close
		int fd4[2];
		pipe(fd4);
		if (pipe(fd4) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid5 = fork();
		if (pid5 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid5 == 0 ) {
			dup2(fd3[0],0);
			close(fd3[0]);
			dup2(fd4[1],1);
			close(fd4[0]);
			close(fd4[1]);
			int exec_return = execlp(argv[5],argv[5],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent 
		wait(NULL);
		close(fd3[0]);
		close(fd4[1]);
		int fd5[2];
		pipe(fd5);
		if (pipe(fd5) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid6 = fork();
		if (pid6 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid6 == 0 ) {
			dup2(fd4[0],0);
			close(fd4[0]);
			dup2(fd5[1],1);
			close(fd5[0]);
			close(fd5[1]);
			int exec_return = execlp(argv[6],argv[6],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent 
		wait(NULL);
		close(fd4[0]);
		close(fd5[1]);
		int fd6[6];
		pipe(fd6);
		if (pipe(fd6) == -1){
			int err = errno;
    		perror("pipe");
    		return err;  //pipe error
		}
		pid_t pid7 = fork();
		if (pid7 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid7 == 0 ) {
			dup2(fd5[0],0);
			close(fd5[0]);
			dup2(fd6[1],1);
			close(fd6[0]);
			close(fd6[1]);
			int exec_return = execlp(argv[7],argv[7],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent 
		wait(NULL);
		close(fd5[0]);
		close(fd6[1]);
		pid_t pid8 = fork();
		if (pid8 == -1) { //fork error
			int err = errno;
			perror("fork failed");
			return err;
		}
		if (pid8 == 0 ) {
			dup2(fd6[0],0);
			close(fd6[0]);
			int exec_return = execlp(argv[8],argv[8],NULL);
  			if (exec_return == -1) {
    			exec_return = errno;
    			perror("execlp failed");
    			return exec_return;
			}
		}
//parent 
		wait(NULL);
		close(fd6[0]);
	}
	return 0;
}
